<template>
  <el-row>
    <ImportExcelModal :visible.sync="visible" fileType="chCargoProductInfo" title="导单"
                      downLoadUrl="/static/excel/商家商品资料导入模板.xlsx">
      <div slot="tips" class="col-xs-12 m--margin-top-10">
        <p class="alert alert-danger font-red text-center">
          由于上传文件转换为订单需要时间处理，上传后文件处理情况请在处理结果中查看，文件处理成功后订单将进入暂存单列表。
        </p>
      </div>
    </ImportExcelModal>

    <ImportResult :visible.sync="resultVisible"
                  url="/ms-common-excelupload/excel/getImportData?tempalateType=chCargoProductInfo"></ImportResult>

    <AddProductModal :visible.sync="addProductModalVisible" :id="id" :cargoOwnerId="cargoOwnerId"></AddProductModal>

    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="name">
                <el-input v-model="search.name" placeholder="商家名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="prin">
                <el-input v-model="search.prin" placeholder="商家联系人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="phone">
                <el-input v-model="search.phone" placeholder="商家联系电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span>
                    </el-button>
                  </el-col>
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="userName">
                <el-input v-model="search.userName" placeholder="账号姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="state">
                <el-select v-model="search.state" clearable placeholder="状态">
                  <el-option
                    label="启用"
                    value="1">
                  </el-option>
                  <el-option
                    label="停用"
                    value="0">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span>
                    </el-button>
                  </el-col>
                  <!--<el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>

    <el-col :span="24">
      <basic-table
        ref="dataTable"
        url="/ms-warehouse-order/basicData/findCargoOwnerList"
        :columns="columns"
        showCheckbox
        :fixedColumns="{
          leftColumns: 2,
          rightColumns: 1
        }"
      >
        <template slot="column" >
          <el-table-column v-if="tabIndex == 1" fixed="right" align="center" width="140px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                商家商品
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="add(scope.row)"
              >
                新增商品
              </el-button>
            </template>
          </el-table-column>

          <el-table-column v-if="tabIndex == 2" fixed="right" align="center" width="100px" label="操作">
            <template slot-scope="scope">
              <a v-if="scope.row.errorFileId" :href="urlPrefix + '/ms-mongodb/file/download?fileId=' +scope.row.errorFileId">
                错误文件下载
              </a>
            </template>
          </el-table-column>
        </template>

        <template slot="buttons">
          <el-button type="primary" @click.prevent="visible = true">导入商家商品</el-button>
          <el-button type="primary" @click.prevent="resultVisible = true">导入结果</el-button>
        </template>
        <span class="font-red" v-if="tabIndex == 2" slot="tips">
          处理结果为"部分导入"的Excel表格正确的行成功导入，只需下载错误文件更改后重新上传(错误文件中只有导入失败的行)
        </span>
      </basic-table>
    </el-col>
  </el-row>
</template>
<script>
import {copy} from '@/utils'
import {urlPrefix} from '@/config/index'
import {unixTimestampFilter} from '@/filters'
import ImportExcelModal from '@/views/oms/ImportExcelModal'
import AddProductModal from '@/views/oms/basicData/modal/AddProductModal'
import ImportResult from '@/views/business/ImportResult'
import StompJs from '@/utils/StompJs'
import {getLoginUser} from '@/utils/ajax'
export default {
  name: 'basicDataManufactorProIndex',
  components: {
    ImportExcelModal,
    AddProductModal,
    ImportResult
  },
  data() {
    return {
      urlPrefix,
      tabIndex: 1,
      search: {
        code: '',
        name: '',
        state: '',
        prin: '',
        phone: ''
      },
      id: '',
      cargoOwnerId: '',
      visible: false,
      resultVisible: false,
      addProductModalVisible: false,
      isOpen: false,
      searchLoading: false,
      subOrderFinish: '',
      columns: [
        {label: '商家编号', data: 'code'},
        {label: '商家名称', data: 'name'},
        {label: '账号姓名', data: 'userName'},
        {label: '绑定登录手机号', data: 'userPhone'},
        {label: '角色', data: 'roleName'},
        {label: '商家联系人', data: 'prin'},
        {label: '商家联系电话', data: 'phone'},
        {
          label: '添加时间',
          data: 'foundTime',
          width: '160',
          render: (data, full) => {
            return unixTimestampFilter(data, 'YYYY-MM-DD HH:mm')
          }
        },
        {label: '操作人', data: 'founderName'},
        {
          label: '状态',
          data: 'state',
          render(data, full) {
            if (data === 1) {
              return '启用'
            } else if (data === 0) {
              return '停用'
            }
            return data
          }
        }
      ]
    }
  },
  created() {
    let s1 = new StompJs()
    let userId = getLoginUser('id')
    s1.subscribe('/topic/' + userId + 'CargoProductFinish', (message) => {
      this.refresh()
    }).then(sub => {
      this.subOrderFinish = sub
    })
  },
  destroyed() {
    if (this.subOrderFinish) {
      this.subOrderFinish.unsubscribe()
    }
  },
  methods: {
    searchFunc(search) {
      let data = copy(search, true)
      this.searchLoading = true
      this.$refs.dataTable.search(data).finally(_ => {
        this.searchLoading = false
      })
    },
    resetFields(searchForm) {
      this.$refs.searchForm.resetFields()
    },
    refresh() {
      this.searchFunc(this.search)
    },
    show(data) {
      this.$router.push({path: '/ss-oms/basic-data/production/show/' + data.id})
    },
    add(data) {
      this.id = ''
      this.cargoOwnerId = data.id
      this.addProductModalVisible = true
    }
  }
}
</script>
